<?php

namespace app\common\logic;

use think\Cache;
use think\Db;

/**
 * 课程标签操作
 * Class SendLogic
 */
class CacheLogic extends Logic
{
    static $expire_time = 7200;
    static $expire_day = 86400;

    /**
     * 获取首页banner缓存
     * @return array|mixed
     */
    public static function get_index_banner_cache()
    {
        $key = config('cache_key.front_banner') ;
        $content = Cache::get($key);
        if (empty($content)) {
            $content = Db::name('banner')->where([
                'state' => 1,
            ])->order('sort desc')
                ->field('thumb,url')->select()->toArray();

            Cache::set($key, $content, self::$expire_time);
        }
        return $content;
    }

    /**
     * 获取首页课程缓存 手机端调用
     */
    public static function get_index_course_cache()
    {
        $content = Cache::get(config('cache_key.front_course'));
        if (empty($content)) {
            $field = true;
            $content = model('Course')->getCourseList([], $field, 'sort desc', 0, 10);
            Cache::set(config('cache_key.front_course'), $content, self::$expire_time);
        }
        return $content;
    }

    /**
     * 获取用户的搜索结果
     * @param array $param
     * @return bool
     */
    public static function get_user_search($param = [])
    {
        $uid = $param['uid'];
        $key = config('cache_key.search_history') . $uid;
        $content = Cache::get($key);
        if (empty($content)) {
            $content = Db::name('user_search')->where('uid', $uid)->order('add_time desc')->limit(0, 4)->column('tags');
            Cache::set($key, $content, self::$expire_time);
        }
        return $content;
    }

    /**
     * 获取章节目录
     * @param array $param
     * @return mixed
     */
    public static function get_catalog($param = [])
    {
        $course_id = $param['course_id'];
        $key = config('cache_key.course_catalog') . $course_id;
        $content = '';//Cache::get($key);
        if (empty($content)) {
            $field = 'l.id,l.course_id,l.parent_id,l.node_id,l.title lesson_title,v.title video_title,v.duration,l.res_id,l.sort';
            $catalog = model('Lesson')->getFullLesson(['l.course_id' => $course_id], $field, 'l.sort asc');
            $lesson_count = model('Lesson')->getLessonCount(['course_id' => $course_id, 'parent_id' => 0]);
            $content = [$catalog, $lesson_count];
            Cache::set($key, $content, self::$expire_day);
        }
        return $content;
    }

    /**
     * 获取常见问题  手机端调用
     * @return mixed
     */
    public static function get_faq_info()
    {
        $key = config('cache_key.faq_info');
        $content = Cache::get($key);
        if (empty($content)) {
            $field = 'ask,answer';
            $content = model('Faq')->getList(['state' => 1], $field);
            Cache::set($key, $content, self::$expire_day);
        }
        return $content;
    }

    /**
     * 获取版本信息
     * @return mixed
     */
    static function get_app_version()
    {
        $key = config('cache_key.app_version');
        $content = Cache::get($key);
        if (empty($content)) {
            $content = Db::name('system')->value('app_version');
            Cache::set($key, $content, self::$expire_time);
        }
        return $content;
    }


}